/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulator;

import java.util.LinkedList;
import java.util.List;
import manager.EventManager;
import manager.PacketGeneratorManager;
import manager.PathManager;
import manager.QueueManager;
import model.PacketGenerator;
import model.Path;
import model.Queue;
import utils.Utils;

/**
 *
 * @author wpc
 */
public class EventSimulator {

    private EventManager em;
    private List<Queue> queueList;

    public void initr1() {
      int K = Utils.K;
        Queue q1 = new Queue(1, 10, 1e11);
        Queue q1e = new Queue(11);

        Path p1_1e = new Path(Utils.steps(q1, q1e));

        PathManager pm = new PathManager();
        pm.addPath(p1_1e);
        QueueManager qm = new QueueManager(pm);
        qm.addQueue(q1);
        qm.addQueue(q1e);

        PacketGeneratorManager gm = new PacketGeneratorManager();

        gm.add(new PacketGenerator(p1_1e, 1e7, Utils.G_SIZE));
        em = new EventManager(gm, qm);

        this.queueList = new LinkedList<Queue>();
        this.queueList.add(q1);
        this.queueList.add(q1e);
        Queue.PATH_MANAGER = pm;
    }

    public void initr2() {
        int K = Utils.K;
        double qRate = 1e11;
        Queue q1a = new Queue(11, K, qRate);
        Queue q1b = new Queue(12, K, qRate);
        Queue q2a = new Queue(21, K, qRate);
        Queue q2b = new Queue(22, K, qRate);
        Queue q3a = new Queue(31, K, qRate);
        Queue q3b = new Queue(32, K, qRate);
        Queue q3c = new Queue(33, K, qRate);
        Queue q4a = new Queue(41, K, qRate);
        Queue q2e = new Queue(20);
        Queue q3e = new Queue(30);
        Queue q4e = new Queue(40);

        Path p1a_2b_4a_4e = new Path(Utils.steps(q1a, q2b, q4a, q4e));
        Path p1b_3b_4a_4e = new Path(Utils.steps(q1b, q3b, q4a, q4e));
        Path p1b_3c_3e = new Path(Utils.steps(q1b, q3c, q3e));
        Path p2b_4a_4e = new Path(Utils.steps(q2b, q4a, q4e));
        Path p3a_2a_2e = new Path(Utils.steps(q3a, q2a, q2e));
        Path p3b_4a_4e = new Path(Utils.steps(q3b, q4a, q4e));
        PathManager pm = new PathManager();

        Path[] pathes = {
            p1a_2b_4a_4e,
            p1b_3b_4a_4e,
            p1b_3c_3e,
            p2b_4a_4e,
            p3a_2a_2e,
            p3b_4a_4e};
        double[] gRates = {0.3,
                           0.2,
                           0.7,
                           0.25,
                           0.8,
                           0.2};

        for(Path path:pathes)
            pm.addPath(path);

        QueueManager qm = new QueueManager(pm);
        
        this.queueList = new LinkedList<Queue>();
        this.queueList.add(q1a);
        this.queueList.add(q1b);
        this.queueList.add(q2a);
        this.queueList.add(q2b);
        this.queueList.add(q3a);
        this.queueList.add(q3b);
        this.queueList.add(q3c);
        this.queueList.add(q4a);
        this.queueList.add(q2e);
        this.queueList.add(q3e);
        this.queueList.add(q4e);

        for(Queue queue:this.queueList)
            qm.addQueue(queue);

        PacketGeneratorManager gm = new PacketGeneratorManager();

        double basicRate = 1.25e7;
        for (int i = 0; i < pathes.length; ++i) {
            gm.add(new PacketGenerator(pathes[i], gRates[i] * basicRate, (long) (gRates[i] * Utils.G_SIZE)));
        }
        em = new EventManager(gm, qm);

        Queue.PATH_MANAGER = pm;
    }

    public void run() {
        em.run();
    }

    public void display_results(){
      if(!Utils.PRINT_QUEUE_DELAY){
        for(Queue q:this.queueList)
            q.getResult();
      }else{
        for(Queue q:this.queueList)
            q.displayQueueDelay();
      }
    }
}
